package com.learn.jdk;

import java.util.concurrent.CompletableFuture;

/**
 * @author: lipan
 * @date: 2021/5/18 20:12
 */

public class CompletableFutureDemo {


    public static void main(String[] args) {

        CompletableFuture<String> completableFuture=new CompletableFuture();

       new Thread(()->{
           String result = doBusi();
           completableFuture.complete(result);
       }).start();

       completableFuture.whenComplete((result,ex)->{
           System.out.println("执行结果:"+result);
           System.out.println("异常信息:"+ex.getLocalizedMessage());
       });

        System.out.println("other operation....");
    }




    //模拟业务逻辑的耗时操作
    private static String doBusi() {

        try {
            System.out.println("begin business operation");
            Thread.sleep(3000);
            System.out.println("end business operation");
            return "finished";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;

    }

}
